﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Collections.ObjectModel;


namespace University.Workshop.Model.LogicalWinner
{
    public class LogicalWinnerUser
    {
        private static LogicalWinnerUser winner;
        public static LogicalWinnerUser Instance { get { if (winner == null)winner = new LogicalWinnerUser(); return winner; } }
        //public Hand[] hands = new Hand[5];         //las 5 manos
        public Collection<Hand> Hands { get; private set; }
        public int PointerUsers { get; set; }                           //puntero de las manos 1-5 

        //lista de posiciones de ganadores
        public string WinnerInHands { get; private set; } 
       //  private int pointerWinnerInHands = 0;      //puntero de lista de ganadores

        public string NumberOfWinner { get;  set; }

        //lista si hay mas de un ganador por carta mayor
        public string HighestNumberOfWinning { get; private set; }
        // private int pointerHighestNumberOfWinning = 0;

        public Collection<int> EqualWinner { get; private set; } 

        public LogicalWinnerUser()
        {
            PointerUsers = 0;
            EqualWinner = new Collection<int>();
            HighestNumberOfWinning = "";
            Hands = new Collection<Hand>();
            WinnerInHands = "-1";
            for (int i = 0; i < 5; i++)
            {
                EqualWinner.Add( -1);
                Hands.Add( new Hand() );
            }
        }
        
        // manda un string num typo

        public void ConvertCards(string cardsOfTable,string cardsOfUser)
        {
            char[] delimiter = { ' ' };
            string[] result1 = new string[12];
            if (cardsOfTable!=null)
            {
                result1 = cardsOfTable.Split(delimiter); //cartas de la mesa    
            }
            
            string[] result2 = new string[22];
            if (cardsOfUser != null)
            {
                result2 = cardsOfUser.Split(delimiter);  //cartar de usuarios
            }
            string outPutNumber=""; //numeros
            string outPutType="";   //typos
            for (int i = 1; i < 11; i=i+2)
            {
                outPutNumber = outPutNumber + " " + result1[i];
            }
            for (int i = 2; i < 11; i=i+2)
            {
                outPutType = outPutType + " " + result1[i];
            }

            string[] outPutNumberUser = new string[10];  // numeros user
            string[] outPutTypeUser = new  string[10];   // typos user
            int pointer = 0;  
                // tenemos hasta 8 en el list
            int pos1 = 1;
            int pos2 = 2;
            for (int i = 0; i < TableManager.Instance.Tables[0].Users; i++)
            {
                if(result2[pos1] != null)
                {
                    for (int j = pos1; j < pos1+4; j=j+2)
                    {
                        outPutNumberUser[pointer] = outPutNumberUser[pointer] + " " + result2[j];
                        
                    }
                    pos1 = pos1 + 4;
                    for (int j = pos2; j < pos2+4; j=j+2)
                    {
                        outPutTypeUser[pointer] = outPutTypeUser[pointer] + " " + result2[j];
                    }
                    pos2 = pos2 + 4;
                }
                pointer = pointer+1;
            }
            for (int i = 0; i < TableManager.Instance.Tables[0].Users ; i++)
            {
                SetHand( outPutNumber + outPutNumberUser[i] , outPutType + outPutTypeUser[i]);    
            }
            
        }
        public void SetHand(string numbers, string type)
        {
            char[] auxiliary = { ' ' };
            string[] vectorNumbers= new string[8];
            if (numbers != null)
            {
                vectorNumbers = numbers.Split(auxiliary);    
            }
            

            for (int i = 1; i < 8; i++)
            {

                Hands[PointerUsers].CardsOfHand[i-1].NumberOfCard = Convert.ToInt32(ConvertNumber(vectorNumbers[i]), CultureInfo.InvariantCulture);
                if (i >= 6)
                {
                    if (i==6)
                    {
                        Hands[PointerUsers].Mayor1 = (Convert.ToInt32(ConvertNumber(vectorNumbers[i]), CultureInfo.InvariantCulture));
                    }
                    if (i==7)
                    {
                        Hands[PointerUsers].Mayor2 = Convert.ToInt32(ConvertNumber(vectorNumbers[i]), CultureInfo.InvariantCulture);
                    }
                    
                }
            }
           
            string[] vectorType = new string[8];
            if (type != null)
            {
                vectorType = type.Split(auxiliary);
            }
            for (int j = 1; j < 8; j++)
            {
                if (vectorType[j] == "Club")
                   Hands[PointerUsers].CardsOfHand[j-1].TypeOfCard = "clover";
                if (vectorType[j] == "Spade")
                    Hands[PointerUsers].CardsOfHand[j-1].TypeOfCard =  "sword";
                if (vectorType[j] == "Heart")
                    Hands[PointerUsers].CardsOfHand[j-1].TypeOfCard = "heart";
                if (vectorType[j] == "Diamond")
                    Hands[PointerUsers].CardsOfHand[j-1].TypeOfCard = "diamont";
            }
            Hands[PointerUsers].HandState = true;
            PointerUsers = PointerUsers + 1;
            
        }
        /*
        public void InsertCard(String card,Int32 position)
        {
            char[] auxiliary = { ' ' };
            string[] result = new string[10];
            if (card != null)
            {
                result = card.Split( auxiliary );
            }
            string number = ConvertNumber(result[0]);
            string type = ConvertType(result[1]);

            
            for (int j = 0; j < 7; j++)
            {
                if (Hands[position].CardsOfHand[j].NumberOfCard == -1)
                {
                    Hands[position].CardsOfHand[j].NumberOfCard = Convert.ToInt32(number,CultureInfo.InvariantCulture);
                    if (type == "t")
                        Hands[position].CardsOfHand[j].TypeOfCard = "clover";
                    if (type == "e")
                        Hands[position].CardsOfHand[j].TypeOfCard = "sword";
                    if (type == "c")
                        Hands[position].CardsOfHand[j].TypeOfCard = "heart";
                    if (type == "d")
                        Hands[position].CardsOfHand[j].TypeOfCard = "diamont";
                }
            }
        }
         * */
        public static string ConvertNumber(string card)
        { 
            
            switch(card)
            {
                case "None":
                    return "1";
                case "Ace":
                    return "14";
                case "Two":
                    return "2";
                case "Three":
                    return "3";
                case "Four":
                    return "4";
                case "Five":
                    return "5";
                case "Six":
                    return "6";
                case "Seven":
                    return "7";
                case "Eight":
                    return "8";
                case "Nine":
                    return "9";
                case "Ten":
                    return "10";
                case "Jack":
                    return "11";
                case "Queen":
                    return "12";
                case "King":
                    return "13";
            }
            return "";
        }

        
        
        public void Restore()
        {
            PointerUsers = 0;
            //pointerHighestNumberOfWinning = 0;
            //pointerWinnerInHands = 0;

            for (int i = 0; i < 5; i++)
            {
                
                HighestNumberOfWinning = "1";
                WinnerInHands = "-1";
                EqualWinner[i] = -1;
            }
        }
        public void GetWinner()
        {
            for (int i = 0; i < 5; i++)
            {
                Hands[i].HandState = TableManager.Instance.Tables[0].States[i];
            } 
            //positionWinner = "1";
            char[] delimiter = { ' ' };
            //vector = positionWinner.Split(delimiter); 

            for (int i = 0; i < TableManager.Instance.Tables[0].Users; i++)
            {
                if (Hands[i].HandState)
                {
                    string[] vector2 = (Hands[i].HandForce()).Split(delimiter);
                    if (Convert.ToInt32(vector2[0], CultureInfo.InvariantCulture) > Convert.ToInt32(NumberOfWinner, CultureInfo.InvariantCulture))
                    {
                        WinnerInHands = i.ToString(CultureInfo.InvariantCulture)+" "+vector2[0]; //pos winner
                        if(vector2.Length == 2)
                        {
                            HighestNumberOfWinning = vector2[1];   // num carta
                        }
                        NumberOfWinner = vector2[0];  //valor de mano
                    }
                    else
                    {
                        if (Convert.ToInt32(vector2[0], CultureInfo.InvariantCulture) == Convert.ToInt32(NumberOfWinner, CultureInfo.InvariantCulture))
                        {
                            if (Convert.ToInt32(vector2[1], CultureInfo.InvariantCulture) > Convert.ToInt32(HighestNumberOfWinning[0], CultureInfo.InvariantCulture))
                            {
                                WinnerInHands = i.ToString(CultureInfo.InvariantCulture)+" "+vector2[0];
                                if (vector2.Length == 2)
                                {
                                    HighestNumberOfWinning = vector2[1];
                                }
                                NumberOfWinner = vector2[0];
                            }
                        }
                    }
                }
            }
        }
    }
}
